package com.lagou.edu.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.dubbo.config.annotation.Reference;
import com.lagou.edu.pojo.LagouAuthCode;
import com.lagou.edu.service.CodeService;
import com.lagou.edu.service.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.service.UserService;

@RestController
@RequestMapping("/api")
public class UserController {

    @Reference
    private CodeService codeService;

    @Reference
    private EmailService emailService;
    @Autowired
    private UserService userService;


    //⽣成验证码并发送到对应邮箱，成功
    //true，失败false
    @GetMapping("/code/create/{email}")
    public Boolean createEmail(@PathVariable String email) {
        //生成6位随机数字
        Double code = (Math.random()*9+1)*100000;
        String code1 = code.toString().substring(0,6);
        System.out.println(code1);
        codeService.codeSave(email,code1);
        emailService.createEmail(email,code1);
        return true;
    }

    //校验超时
    @GetMapping("/validate/{email}/{code}")
    public Integer validateEmail(@PathVariable String email,@PathVariable String code) {
        LagouAuthCode lagouAuthCode = new LagouAuthCode();
        lagouAuthCode.setEmail(email);
        lagouAuthCode.setCode(code);
        Example<LagouAuthCode> example = Example.of(lagouAuthCode);
        lagouAuthCode = codeService.findOne(example);
        if(lagouAuthCode==null){
            return 1;
        }else if(lagouAuthCode.getExpiretime().before(new Date())){
            return 2;
        }else{
            return 0;
        }
    }

    //
    @GetMapping("/register/{email}/{password}/{code}")
    public Map<String,String> registerUser(@PathVariable("email") String email,
                @PathVariable("password") String password,
                @PathVariable("code") String code, HttpServletResponse response) {
        Map<String,String> resultMap = new HashMap<>();
       int validateResult = emailService.validateEmail(email,code);
        if(validateResult==1){
            resultMap.put("message","数据不存在");
            resultMap.put("success","false");
        }else if(validateResult==2){
            resultMap.put("message","验证码超时");
            resultMap.put("success","false");
        }else{
            resultMap.put("message","成功");
            resultMap.put("success","true");
            userService.saveToken(email,password);
          Cookie cookie = new Cookie("token",email+"_"+password);
          cookie.setPath("/");
            response.addCookie(cookie);
            return resultMap;
        }

        return resultMap;
    }


  @GetMapping("/login/{email}/{password}")
  public Map<String,String> login(@PathVariable("email") String email,
      @PathVariable("password") String password,
      HttpServletResponse response) {
    Map<String,String> resultMap = new HashMap<>();
    String token = email+"_"+password;
    LagouToken lagouToken = userService.findOne(token);
    if(lagouToken==null){
      resultMap.put("message","邮箱或密码错误");
      resultMap.put("success","false");
    }else{
      Cookie cookie = new Cookie("token",email+"_"+password);
      cookie.setPath("/");
      response.addCookie(cookie);
      resultMap.put("message","成功");
      resultMap.put("success","true");
    }

    return resultMap;
  }


  @GetMapping("/info/{token}")
  public Map<String,String> userInfo(@PathVariable("token") String token) {
    Map<String,String> resultMap = new HashMap<>();
    LagouToken lagouToken = userService.findOne(token);
    if(lagouToken==null){
      resultMap.put("message","token错误");
      resultMap.put("success","false");
    }else{
      resultMap.put("message",lagouToken.getEmail());
      resultMap.put("success","true");
    }

    return resultMap;
  }
}
